/*
* Updates HHI and scatter graph in the paper, using the longer time-series
*
* Adam Feb 26 2024
*
*/

clear

// set scheme frbny
// cd to the current directory where you store this file 
* global triggers
global HHI_chart =0		// =1 produces figure of HHI and balances over time
global scatter_figure =1	// =1 produces the scatter figures (twin panel) of timings and rates


//
// Creates twin figures of payment timing (receives and sends) and rates
//
if $scatter_figure >0 {
	
  import delimited using Data2023_04.csv, clear
   
  keep tdiff50_recv1 tdiff50_send1 repo_balances1 ao_balances1 sofr_ioer quarterend total_issuance_all total_redemptions_all ///
       total_issuance_bills coupon_issuances tbills_outstanding corporation_income_taxes_trillio netposition_try ///
       tdiff50_recvf1 tdiff25_recvf1 tdiff75_recvf1 date deposits
  
  replace tdiff50_recv1 = "." if tdiff50_recv1=="NA"
  replace tdiff50_send1 = "." if tdiff50_send1=="NA"  
  replace repo_balances1 = "." if repo_balances1=="NA"  
  replace ao_balances1 = "." if ao_balances1=="NA"  
  replace total_issuance_all = "." if total_issuance_all=="NA"  
  replace total_redemptions_all = "." if total_redemptions_all=="NA"  
*  replace sofr_ioer = "." if sofr_ioer=="NA"  
  replace total_issuance_bills = "." if total_issuance_bills=="NA"  
  replace coupon_issuances = "." if coupon_issuances=="NA"  
  replace tbills_outstanding = "." if tbills_outstanding=="NA" 
  replace corporation_income_taxes_trillio = "." if corporation_income_taxes_trillio=="NA"  
  replace netposition_try = "." if netposition_try=="NA"  
  replace tdiff25_recvf1 = "." if tdiff25_recvf1=="NA"  
  replace tdiff50_recvf1 = "." if tdiff50_recvf1=="NA"  
  replace tdiff75_recvf1 = "." if tdiff75_recvf1=="NA"  
  replace deposits = "." if deposits=="NA"  

  destring , replace
  
  scatter sofr_ioer tdiff50_recv1 if sofr_ioer<300, msize(tiny) ///
  ytitle("SOFR-IOR (basis points)") xtitle("Receiving time from sample mean (minutes)") ///
  xlabel(-100(50)150)
  
  graph save output/receivechart, replace
	
  scatter sofr_ioer tdiff50_send1 if sofr_ioer<300, msize(tiny) mcolor(red) ///
  ytitle("") xtitle("Sending time from sample mean (minutes)") xlabel(-100(50)150)	
	
  graph save output/sendchart, replace	
  
  graph combine output/receivechart.gph output/sendchart.gph, name(paperchart, replace)
  graph export output/ratetime_figure.pdf, as(pdf) replace
  
}



//
// Creates the HHI chart (figure 7)
//
if $HHI_chart >0 {

  use daily_HHI_set1_March2023, clear
  
  twoway (line open_balance Date) (line balHHI Date, lpattern(dash) yaxis(2)), ///
  legend( position(6) rows(1) label(1 "Balances") label(2 "HHI")) ///
  xtitle("") ytitle("Large dealer balances ($ billions)") ytitle("Herfindahl–Hirschman Index", axis(2)) ///
  tlabel( ,format(%tdCCYY)) tline(17mar2020, lcolor(black)) tline(17sep2019, lcolor(black))
  
  graph export output/HHI_extended.pdf, as(pdf) replace
	
	
	
}


